package com.qidian.buy.item.receives;

import com.qidian.buy.dto.ItemMessageDTO;
import com.qidian.buy.item.service.ItemService;
import com.qidian.buy.util.Tools;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.io.IOException;

/**
 * Author: 戴着假发的程序员
 * Date: 2024-05-15 14:48
 * company: 起点编程
 **/
@Component
public class ItemOptionReceive implements ChannelAwareMessageListener {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ItemService itemService;
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        // 获取消息的唯一性ID
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        ItemMessageDTO dto = null;
        try {
            String msg = new String(message.getBody(),"UTF-8");
            dto = Tools.json2object(msg, ItemMessageDTO.class);
            // 手动确认
            channel.basicAck(deliveryTag,true);
            // 理论上这里也要根据不同的option做不同的操作。
            // 我们这里暂时只做了页面生成
            itemService.getStaticForDetail(dto.getItemId());
        } catch (IOException e) {
            // 重试.....
            throw new RuntimeException(e);
        }
    }
}
